package com.ohosadvance.tsnackbar;

import com.ohosadvance.topsnackbar.utils.LogUtil;
import com.ohosadvance.topsnackbar.TSnackbar;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

/**
 * ToolbarAbilityExample
 *
 * @since 2021/7/2
 */
public class ToolbarAbilityExample extends Ability {
    DirectionalLayout toolbar;
    private BGButton button6;
    private BGButton button5;
    private BGButton button4;
    private BGButton button3;
    private BGButton button2;
    private BGButton button1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_toolbar_ability_example);
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_status_title));
        initView();
    }

    private void initView() {
        button1 = (BGButton) findComponentById(ResourceTable.Id_button_example_1);
        button2 = (BGButton) findComponentById(ResourceTable.Id_button_example_2);
        button3 = (BGButton) findComponentById(ResourceTable.Id_button_example_3);
        button4 = (BGButton) findComponentById(ResourceTable.Id_button_example_4);
        button5 = (BGButton) findComponentById(ResourceTable.Id_button_example_5);
        button6 = (BGButton) findComponentById(ResourceTable.Id_button_example_6);
        toolbar = (DirectionalLayout) findComponentById(ResourceTable.Id_toolbar);
    }

    private void setButton1Click() {
        button1.setClickedListener(component -> {
            TSnackbar.make(toolbar, "Hello from VSnackBar 1", TSnackbar.LENGTH_LONG)
                    .show();
        });
    }

    private void setButton2Click() {
        button2.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(toolbar, "Had a snack at Snackbar", TSnackbar.LENGTH_LONG)
                    .setAction("UNDO", component1 -> {
                        LogUtil.loge("onClick triggered");
                    });
            snackbar.setActionTextColor(ResourceTable.Color_ltgray);
            snackbar.setIconLeft(ResourceTable.Media_ic_core, 24);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#555555ff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.WHITE);
            snackbar.show();
        });
    }

    private void setButton3Click() {
        button3.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(toolbar, "Had a snack at Snackbar", TSnackbar.LENGTH_LONG)
                    .setAction("ACTION", component1 -> {
                        LogUtil.loge("CLICKED Action");
                    });
            snackbar.setActionTextColor(ResourceTable.Color_white);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#0000CCff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.WHITE);
            snackbar.show();
        });
    }

    private void setButton4Click() {
        button4.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(toolbar, "Had a snack at Snackbar  Had a snack at Snackbar  " +
                            "Had a snack at Snackbar Had a snack at Snackbar Had a snack at Snackbar " +
                            "Had a snack at Snackbar", TSnackbar.LENGTH_LONG);
            snackbar.setActionTextColor(ResourceTable.Color_white);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#CC00CCff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.YELLOW);
            snackbar.show();
        });
    }

    private void setButton5Click() {
        button5.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(toolbar, "Snacking with VectorDrawable", TSnackbar.LENGTH_LONG);
            snackbar.setActionTextColor(ResourceTable.Color_white);
            snackbar.setIconLeft(ResourceTable.Media_ic_core, 24);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#CC00CCff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.YELLOW);
            snackbar.show();
        });
    }

    private void setButton6Click() {
        button6.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(toolbar, "Snacking Left & Right", TSnackbar.LENGTH_LONG);
            snackbar.setActionTextColor(ResourceTable.Color_white);
            // Size in dp - 24 is great!
            snackbar.setIconLeft(ResourceTable.Media_ic_core, 24);
            // Resize to bigger dp
            snackbar.setIconRight(ResourceTable.Media_icon, 48);
            snackbar.setIconPadding(8);
            snackbar.setMaxWidth(3000);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#CC00CCff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.YELLOW);
            snackbar.show();
        });
    }
}
